package com.shux.common.init;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import com.shux.utils.other.SysConfigUtil;

/**
 **********************************************
 *  描述：
 * Simba.Hua
 * 2017年7月30日
 **********************************************
**/
public class InitSysConfigProcessor implements ApplicationListener<ContextRefreshedEvent> {
    Logger logger  = LoggerFactory.getLogger(getClass());
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if(event.getApplicationContext().getParent() != null){
            return;
        }
       ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
       if(classLoader == null){
           classLoader = this.getClass().getClassLoader();
       }
       InputStream inputStream = classLoader.getResourceAsStream("sysConfig.properties");
       Properties property = new Properties();
       try {
        property.load(inputStream);
      } catch (IOException e) {
          logger.error("load sysConfig.properties fail!",e);
      }
       Set<Entry<Object, Object>> entrySet = property.entrySet();
       entrySet.forEach(entry ->{
           SysConfigUtil.set((String)entry.getKey(), entry.getValue());
       });
    }

}
